package com.example.monitoringsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.monitoringsystem.bean.*;
import com.example.monitoringsystem.mapper.PrescribeMapper;
import com.example.monitoringsystem.mapper.PrescriptionMapper;
import com.example.monitoringsystem.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PrescribeServiceImpl implements PrescribeService {
    @Autowired
    private PrescribeMapper prescribeMapper;

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Autowired
    private PrescriptionService prescriptionService;

    @Autowired
    private UserService userService;

    @Autowired
    private MedicineService medicineService;

    @Autowired
    private StatesService statesService;


    //医生开处方
    @Override
    public void prescribe(int patientId, int prescriptionId, Prescription prescription) {
        prescription.setPrescriptionId(prescriptionId);
        prescriptionService.prescribe(prescription);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Date today = new Date();
        Prescribe prescribe = new Prescribe(user.getId(), patientId, prescription.getPrescriptionId(), today);
        prescribeMapper.insert(prescribe);
    }
    //一次开所有处方
    @Override
    public void allPrescribe(int patientId, List<Prescription> prescriptions){
        List<Prescribe> prescribes = prescribeMapper.selectList(null);
        int id = prescribes.get(prescribes.size() - 1).getPrescriptionId() + 1;
        for (int i = 0;i<prescriptions.size();i++){
            this.prescribe(patientId,id,prescriptions.get(i));
        }
    }

    //病人查看自己的处方
    @Override
    public Map showPatientPrescription(){
        Map<String, Object> map = new HashMap<>();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<Prescribe> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", user.getId());
        List<Prescribe> prescribes = prescribeMapper.selectList(wrapper);
        //根据病人id查出处方状态
        States states = statesService.findStatesById(user.getId());
        //根据医生id查出医生姓名
        List<User> doctors = new ArrayList<>();
        User doctor = new User();
        //根据处方id查询处方剂量及注意事项
        List<Prescription>prescriptions = new ArrayList<>();
        Prescription prescription = new Prescription();
        //根据药物id查询药物名字
        List<Medicine> medicines = new ArrayList<>();
        Medicine medicine = new Medicine();
        for (int i = 0;i<prescribes.size();i++){
            doctor = userService.findDoctorById(prescribes.get(i).getDoctorId());
            doctors.add(doctor);
            prescription = prescriptionService.findPrescriptionById(prescribes.get(i).getPrescriptionId());
            prescriptions.add(prescription);
            medicine = medicineService.showMedicine(prescription);
            medicines.add(medicine);
        }
        map.put("prescribe",prescribes);
        map.put("states",states);
        map.put("doctor",doctors);
        map.put("prescription",prescriptions);
        map.put("medicine", medicines);
        return map;
    }



}
